<template>
    <div class="blockTitle pt10" style="margin-bottom: 0;">
        首页 - 游戏库
    </div>
    <nut-searchbar v-model="gameName">
        <template #leftin>
            <search2 style="color: #fff;"></search2>
        </template>
        <template #rightout>
            <span class="text-white" @click="searchtypeShow = true">筛选条件</span>
        </template>
    </nut-searchbar>
    <!-- 首页轮播 -->
    <nut-swiper :init-page="2" :auto-play="3000" pagination-visible pagination-color="#426543"
                pagination-unselected-color="#808080">
        <nut-swiper-item v-for="(item, index) in bannerList" :key="index" style="height: 16vh">
            <image-box :src="item.image" draggable="false"></image-box>
        </nut-swiper-item>
    </nut-swiper>

    <div class="game-type-list">
        <div class="game-type-list-all">
            <div class="all-item active">
                全部游戏
            </div>
            <div class="all-item">
                最多人玩
            </div>
            <div class="all-item">
                经典游戏
            </div>
            <div class="all-item">
                评价最高
            </div>
        </div>
        <div class="game-type-selected">
            <div v-for="(item, index) in searchList" @click="removeSearch(item)" :key="index" class="selected-item">
                {{ item?.split('_')[0] }}
                <close-little width="8" class="icon"></close-little>
            </div>
        </div>
    </div>

    <nut-divider style="color:  #2d2d2d;padding: 0 2%;margin-bottom: 10px;"></nut-divider>

    <div class="recommend-title">
        您可以立即免费游玩这些推出的最新游戏
        <span class="recommend-select" @click="selectType">最新上架
            <rect-down class="ml5"></rect-down>
        </span>
    </div>
    <!-- 推荐列表 -->
    <div class="recommend-list">
        <div v-for="(item, index) in gameList" :key="index" class="recommend-item" @click="clickGame(item)">
            <div class="recommend-item-img">
                <img :src="item.cover" alt="" style="width: 100%;">
            </div>
            <p class="recommend-item-title">{{ item.name }}</p>
            <p class="recommend-item-desc">棋牌对战与角色扮演</p>
            <p class="recommend-item-status inyourLib">
                <span class="icon">
                    <rect-down width="8" style="margin-top: -2px;"></rect-down>
                </span>
                在您的游戏库中
            </p>
        </div>
        <!--        <div class="recommend-item">-->
        <!--            <div class="recommend-item-img">-->
        <!--                <img src="../../assets/710@3x.webp" alt="" style="width: 100%;">-->
        <!--            </div>-->
        <!--            <p class="recommend-item-title">Thrones and Echo hello</p>-->
        <!--            <p class="recommend-item-desc">角色扮演</p>-->
        <!--            <p class="recommend-item-status inyourLib">-->
        <!--                <span class="icon">-->
        <!--                    <rect-down width="8" style="margin-top: -2px;"></rect-down>-->
        <!--                </span>-->
        <!--                在您的游戏库中-->
        <!--            </p>-->
        <!--        </div>-->
        <!--        <div class="recommend-item">-->
        <!--            <div class="recommend-item-img">-->
        <!--                <img src="../../assets/711@3x.webp" alt="" style="width: 100%;">-->
        <!--            </div>-->
        <!--            <p class="recommend-item-title">Aeons Echo</p>-->
        <!--            <p class="recommend-item-desc">棋牌对战与角色扮演</p>-->
        <!--            <p class="recommend-item-status newest">-->
        <!--                <span class="icon">N</span>-->
        <!--                <span class="icon-text">-->
        <!--                    最新-->
        <!--                </span>-->
        <!--            </p>-->
        <!--        </div>-->
        <!--        <div class="recommend-item">-->
        <!--            <div class="recommend-item-img">-->
        <!--                <img src="../../assets/711@3x.webp" alt="" style="width: 100%;">-->
        <!--            </div>-->
        <!--            <p class="recommend-item-title">Aeons Echo</p>-->
        <!--            <p class="recommend-item-desc">角色扮演</p>-->
        <!--            <p class="recommend-item-status goodest">-->
        <!--                <span class="icon">G</span>-->
        <!--                <span class="icon-text">-->
        <!--                    佳评如潮-->
        <!--                </span>-->
        <!--            </p>-->
        <!--        </div>-->
        <!--        <div class="recommend-item">-->
        <!--            <div class="recommend-item-img">-->
        <!--                <img src="../../assets/712@3x.webp" alt="" style="width: 100%;">-->
        <!--            </div>-->
        <!--            <p class="recommend-item-title">Aeons Echo</p>-->
        <!--            <p class="recommend-item-desc">棋牌对战与角色扮演</p>-->
        <!--            <p class="recommend-item-status classic">-->
        <!--                <span class="icon">C</span>-->
        <!--                <span class="icon-text">-->
        <!--                    经典-->
        <!--                </span>-->
        <!--            </p>-->
        <!--        </div>-->
        <!--        <div class="recommend-item">-->
        <!--            <div class="recommend-item-img">-->
        <!--                <img src="../../assets/710@3x.webp" alt="" style="width: 100%;">-->
        <!--            </div>-->
        <!--            <p class="recommend-item-title">Aeons Echo</p>-->
        <!--            <p class="recommend-item-desc">棋牌对战与角色扮演</p>-->
        <!--            <p class="recommend-item-status favorite">-->
        <!--                <span class="icon">H</span>-->
        <!--                <span class="icon-text">-->
        <!--                    最多人玩-->
        <!--                </span>-->
        <!--            </p>-->
        <!--        </div>-->
    </div>

    <p class="recommend-count">
        - 共 <span style="color: #fff;font-weight: 600;"> {{ gameList.length || 0 }}
        </span>款遊戲符合筛选 -
    </p>

    <nut-action-sheet v-model:visible="show" :menu-items="menuItems" @choose="choose"></nut-action-sheet>


    <nut-overlay v-model:visible="searchtypeShow" :close-on-click-overlay="false">
        <div class="overlay-body">
            <div class="overlay-content">
                <serch-type @close-window="closeHandler"></serch-type>
            </div>
        </div>
    </nut-overlay>
</template>

<script setup lang="ts">
import {computed, ref} from 'vue'
import banner1 from '../../assets/708@3x.webp'
import banner2 from '../../assets/708@3x.webp'
import { Search2, RectDown, CloseLittle } from '@nutui/icons-vue'
import SerchType from './components/serch.vue'
import { axiosService } from '@http'
import { useRouter } from 'vue-router'

export interface GameBannerInterface {
    image: string
    link: string
    title: string
    description: any
}
export interface GameListInterface {
    items: GameItem[]
    total_items: number
    limit: number
    first: number
    previous: number
    current: number
    next: number
    last: number
}

export interface GameItem {
    id: number
    name: string
    cover: string
    category_ids: string
    tag_ids: string
    platform: string
}


const show = ref(false)

const gameName = ref('')
const val = ref('')
const searchtypeShow = ref(false)
const bannerList = ref<GameBannerInterface[]>([])
const gameList = ref<GameItem[]>([])
const searchList = ref([])
const platform = ref()
const category_ids = ref()
const tag_ids = ref()

const menuItems = [
    {
        name: '最新'
    },
    {
        name: '好评'
    },
    {
        name: '经典'
    },
    {
        name: '最多人玩'
    }
]


const selectType = () => {
    show.value = true
}
const choose = (item) => {
    val.value = item.name
}

const closeHandler = (data?:any) => {
    searchList.value = [...data.platform,...data.category_ids,...data.tag_ids]
    platform.value = data.platform
    category_ids.value = data.category_ids.map((item:any) =>item.split('_')[1])
    tag_ids.value = data.tag_ids.map((item:any) =>item.split('_')[1])
    getGameList(platform.value.join(','),category_ids.value.join(','),tag_ids.value.join(','))
    searchtypeShow.value=false
}

const removeSearch = (item:any) => {
    const sIndex = searchList.value.findIndex((citem:any) => citem === item)
    searchList.value.splice(sIndex, 1)
    if(item.includes('category')){
        const idx = item.includes('_')? item.split('_')[1]: item
        const fIndex = category_ids.value.findIndex((citem:any) => citem === idx)
        category_ids.value.splice(fIndex, 1)
    }else if(item.includes('tag')){
        const idx = item.includes('_')? item.split('_')[1]: item
        const fIndex = tag_ids.value.findIndex((citem:any) => citem === idx)
        tag_ids.value.splice(fIndex, 1)
    }else{
        const fIndex = platform.value.findIndex((citem:any) => citem === item)
        platform.value.splice(fIndex, 1)
    }
    getGameList(platform.value.join(','),category_ids.value.join(','),tag_ids.value.join(','))

}

const getBanner=()=>{
    axiosService.post<GameBannerInterface[]>('/api/Game/advertiList',{}).then(res=>{
        bannerList.value=res.data||[]
    })
}

const getGameList = (platform='',category_ids='',tag_ids='')=>{
    const data = {
        page: 1,
        size: 20,
        category_ids,
        tag_ids,
        platform,
        sort:1
    }
    axiosService.post<GameListInterface>('/api/Game/gameList',data).then(res=>{
        gameList.value=res.data.items || []
    })
}


getBanner()
getGameList()

const router = useRouter()
const clickGame = (item) => {
    router.push('/gameDetail?id=' + item.id)
}
</script>

<style scoped lang="scss">
.recommend-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 10px;
    color: #fff;

    .recommend-item {
        /* 每个子元素占据50%的宽度减去间距 */
        width: calc(33% - 4px);
        /* 每个子元素占据50%的宽度减去一半的间距 */
        box-sizing: border-box;
        // background-color: lightblue;
        text-align: center;
        // padding: 20px;
        font-size: 12px;
        margin-bottom: 10px;

        img {
            width: 100%;
        }
    }
}

.text-white {
    color: #fff;
    border: 1px solid #fff;
    padding: 2px 15px;
    border-radius: 15px;
}

.recommend-item-title {
    font-size: 14px;
    font-weight: 600;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 120px;
}

.recommend-item-desc {
    text-align: left;
    font-size: 12px;
    color: #b4b4b4;
    line-height: 17px;
}

.recommend-item-status {
    text-align: left;
    font-size: 12px;
    color: #b4b4b4;
    line-height: 17px;
    display: flex;
    align-items: center;
    width: 100%;

    .icon {
        display: inline-block;
        width: 12px;
        height: 11px;
        text-align: center;
        line-height: 12px;
        border-radius: 50%;
        color: #fff;
        font-size: 7px;
        margin-right: 5px;
    }

    &.inyourLib {
        .icon {
            background-color: green;
        }
    }

    &.newest {
        color: #e43b3b;

        .icon {
            background-color: #e43b3b;
        }
    }

    &.goodest {
        color: #e5276c;

        .icon {
            background-color: #e5276c;
        }
    }

    &.classic {
        color: #4f89ff;

        .icon {
            background-color: #4f89ff;
        }
    }

    &.favorite {
        color: #f25702;

        .icon {
            background-color: #f25702;
        }
    }
}

.recommend-title {
    color: #8d8d8d;
    margin-bottom: 10px;
    padding-left: 10px;
    font-size: 12px;
    padding-right: 10px;

    .recommend-select {
        color: #fff;
        font-size: 14px;
        float: right;
        display: flex;
        align-items: center;
    }
}

.game-type-list {
    padding-left: 10px;
    padding-top: 15px;

    .game-type-list-all {
        overflow-x: auto;

        .all-item {
            display: inline-block;
            margin-right: 10px;
            width: 92px;
            height: 25px;
            line-height: 25px;
            font-size: 12px;
            text-align: center;
            border: 1px solid #fff;
            color: #fff;
            border-radius: 15px;

            &.active {
                border: none;
                background-image: linear-gradient(to left, #f900ff, #00cefa);
            }
        }
    }

    .game-type-selected {
        padding-top: 5px;

        .selected-item {
            display: inline-block;
            position: relative;
            width: 74px;
            height: 20px;
            line-height: 20px;
            font-size: 10px;
            text-align: left;
            padding-left: 10px;
            border: 1px solid #fff;
            color: #fff;
            border-radius: 10px;
            margin-right: 10px;

            .icon {
                position: absolute;
                top: 1px;
                right: 10px;
            }
        }
    }
}

.recommend-count {
    text-align: center;
    color: #8d8d8d;
    margin: 20px 0;
}

.overlay-body {
  display: flex;
  height: 100%;
  align-items: center;
  justify-content: center;
}
.overlay-content {
  display: flex;
  width: 100%;
  height: 100%;
  background: #1c1c1c;
  padding: 20px;
//   align-items: center;
//   justify-content: center;
  color: #fff;
}
</style>
